package com.ruoyi.service.service.impl;

import java.math.BigDecimal;
import java.util.List;

import com.ruoyi.archive.domain.ArchiveRoom;
import com.ruoyi.archive.mapper.ArchiveRoomMapper;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.service.domain.ServiceCharge;
import com.ruoyi.service.mapper.ServiceChargeMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.service.mapper.ServiceTenancyMapper;
import com.ruoyi.service.domain.ServiceTenancy;
import com.ruoyi.service.service.IServiceTenancyService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 租赁信息Service业务层处理
 * 
 * @author yepanpan
 * @date 2025-04-21
 */
@Service
public class ServiceTenancyServiceImpl implements IServiceTenancyService 
{
    private static final Logger log = LoggerFactory.getLogger(ServiceTenancyServiceImpl.class);

    @Autowired
    private ServiceTenancyMapper serviceTenancyMapper;
    @Autowired
    private ServiceChargeMapper serviceChargeMapper;
    @Autowired
    private ArchiveRoomMapper archiveRoomMapper;
    /**
     * 查询租赁信息
     * 
     * @param id 租赁信息主键
     * @return 租赁信息
     */
    @Override
    public ServiceTenancy selectServiceTenancyById(Long id)
    {
        return serviceTenancyMapper.selectServiceTenancyById(id);
    }

    /**
     * 查询租赁信息列表
     * 
     * @param serviceTenancy 租赁信息
     * @return 租赁信息
     */
    @Override
    public List<ServiceTenancy> selectServiceTenancyList(ServiceTenancy serviceTenancy)
    {
        return serviceTenancyMapper.selectServiceTenancyList(serviceTenancy);
    }

    /**
     * 新增租赁信息
     * 
     * @param serviceTenancy 租赁信息
     * @return 结果
     */
    @Override
    @Transactional
    public int insertServiceTenancy(ServiceTenancy serviceTenancy)
    {
        if(serviceTenancy.getDeptId() == null){
            ArchiveRoom archiveRoom = archiveRoomMapper.selectArchiveRoomById(serviceTenancy.getRoomId());
            serviceTenancy.setDeptId(archiveRoom.getDeptId());
        }
        serviceTenancy.setCreateTime(DateUtils.getNowDate());

        //先生成收费单
        ServiceCharge serviceCharge = new ServiceCharge();
        BeanUtils.copyProperties(serviceTenancy, serviceCharge);
        serviceCharge.setFeeType(ServiceCharge.TYPE_TENANCY);
        serviceCharge.setStatus(serviceTenancy.getChargeStatus());
        serviceCharge.setFeeShould(serviceTenancy.getFeeTotal());
        serviceCharge.setFeeReal(serviceTenancy.getFeeTotal());
        serviceCharge.genNum();
        serviceChargeMapper.insertServiceCharge(serviceCharge);

        serviceTenancy.setChargeId(serviceCharge.getId());

        //押金
        serviceCharge = new ServiceCharge();
        BeanUtils.copyProperties(serviceTenancy, serviceCharge);
        serviceCharge.setFeeType(ServiceCharge.TYPE_DEPOSIT);
        serviceCharge.setStatus(serviceTenancy.getChargeStatus());
        serviceCharge.setFeeShould(serviceTenancy.getFeeDeposit());
        serviceCharge.setFeeReal(serviceTenancy.getFeeDeposit());
        serviceCharge.setId(null);
        serviceCharge.setNum(null);
        serviceCharge.genNum();
        serviceChargeMapper.insertServiceCharge(serviceCharge);

        return serviceTenancyMapper.insertServiceTenancy(serviceTenancy);
    }

    /**
     * 修改租赁信息
     * 
     * @param serviceTenancy 租赁信息
     * @return 结果
     */
    @Override
    public int updateServiceTenancy(ServiceTenancy serviceTenancy)
    {
        serviceTenancy.setUpdateTime(DateUtils.getNowDate());
        return serviceTenancyMapper.updateServiceTenancy(serviceTenancy);
    }

    /**
     * 批量删除租赁信息
     * 
     * @param ids 需要删除的租赁信息主键
     * @return 结果
     */
    @Override
    public int deleteServiceTenancyByIds(Long[] ids)
    {
        return serviceTenancyMapper.deleteServiceTenancyByIds(ids);
    }

    /**
     * 删除租赁信息信息
     * 
     * @param id 租赁信息主键
     * @return 结果
     */
    @Override
    public int deleteServiceTenancyById(Long id)
    {
        return serviceTenancyMapper.deleteServiceTenancyById(id);
    }

}
